<?php

namespace app\nideshop\controller;

use think\Controller;

class Base extends Controller
{
	protected $params;

	/**
	 * 获取请求参数
	 * @Author   Yichen
	 * @DateTime 2020-05-18T23:38:41+0800
	 * @return   [type]                   [description]
	 */
	protected function initialize()
	{
		if (request()->isPost()) {
			$this->params = request()->param();
			if (empty($this->params)) {
				$data = file_get_contents('php://input');
				$this->params = json_decode($data, true);
			}
		} else {
			$this->params = request()->get();
		}
	}

	/**
	 * 验证参数
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-24T09:12:28+0800
	 * @param    [object]                   $validates [验证器对象]
	 * @param    [string]                   $scene     [验证场景]
	 * @param    [array]                    $data      [验证参数]
	 * @return   [type]                              [description]
	 */
	protected function checkParam($validates, $scene, $data)
	{
		if (!$validates->scene($scene)->check($data)) {
			return $validates->getError();
		}
		return true;
	}

	/**
	 * curl发送get/post请求
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-24T08:54:00+0800
	 * @param    [string]                   $url  [请求地址]
	 * @param    [array]                    $data [请求参数]
	 * @return   [type]                         [description]
	 */
	protected function http_request($url, $data = null)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		if (!empty($data)) {
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}

	/**
	 * curl发送post请求json数据
	 * @Author   Yichen
	 * @DateTime 2020-05-24T08:55:47+0800
	 * @param    [string]                   $url  [description]
	 * @param    [json]                     $data [description]
	 * @return   [type]                         [description]
	 */
	protected function http_post_json($url, $data)
	{
	    $data = json_encode($data);
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	    curl_setopt($ch, CURLOPT_POST, 1);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    curl_setopt($ch, CURLOPT_HEADER, 0);
	    $header = empty($header) ? ['Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data)] : $header;
	    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    $result = curl_exec($ch);
	    $error = curl_errno($ch);
	    if ($error) return ['errno' => false, 'errmsg' => $error];
	    curl_close($ch);
	    return json_decode($result, true);
	}


	/**
	 * 生成订单号
	 * @Author   Yichen
	 * @DateTime 2020-05-04T19:03:20+0800
	 * @return   [type]                   [description]
	 */
	protected function createOrderNo()
	{
		$order_id_main = date('YmdHis') . rand(10000000,99999999);
  		$order_id_len = strlen($order_id_main);
		$order_id_sum = 0;
		for($i=0; $i<$order_id_len; $i++){
			$order_id_sum += (int)(substr($order_id_main,$i,1));
		}
		$osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
		return $osn;
	}

}